<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event,Auth;
class XzController extends Controller
{
    public function messages($table){
        if($table == 'jrjg'){
            return [
                'name.required' => '项目名称不能为空',
                'unit_id.required'  => '申请单位不能为空',
                'up_unit.required' => '上级主管单位不能为空',
                'gclb.required' => '工程类别不能为空',
                'gcsx.required' => '工程属性不能为空',
                'fxdj.required' => '风险等级不能为空',
                'fhjb.required' => '防护级别不能为空',
                'bjsj.required' => '报建时间不能为空',
                'spsj.required' => '审批时间不能为空',
                'yssj.required' => '验收时间不能为空',
                'is_ok.required' => '达标情况不能为空',
            ];
        }
    }

    public function xzindex(request $request)
    {   

        if($request->rows) {


        if($request->gclb){
            $map['jrjg.gclb'] = $request->gclb;
        }

        if($request->gcsx){
            $map['jrjg.gcsx'] = $request->gcsx;
               
        }

        if($request->is_ok){

            if ( $request->is_ok == 3) {
                 $map['jrjg.is_ok'] = 0;
            }else{
                $map['jrjg.is_ok'] = $request->is_ok;
            }
            
        }



        if($request->name){
            $map[] = array('jrjg.name','like','%'.$request->name.'%');
        }



        $map['jrjg.is_del'] = '0';


        $res = DB::table('jrjg')->where($map)
            ->leftJoin('units', 'units.id', '=', 'jrjg.unit_id')
            ->select('jrjg.*', 'units.name as unitname')
            ->paginate($request->rows);
        $list = $res->items();
        foreach ($list as $k=>$v){
            if($v->gclb == 1){
                $list[$k]->gclb = '营业场所';
            }elseif($v->gclb == 2){
                $list[$k]->gclb = '金库';
            }elseif($v->gclb == 3){
                $list[$k]->gclb = '自助银行';
            }elseif($v->gclb == 4){
                $list[$k]->gclb = '自助机器';
            }else{
                $list[$k]->gclb = '未定义';
            }


            if ($v->gcsx == 1) {
                 $list[$k]->gcsx = '新建';
            }elseif ($v->gcsx == 2) {
                $list[$k]->gcsx = '改建';
            }else{
                $list[$k]->gcsx = '未定义';
            }


            if ($v->is_ok == 1) {
                 $list[$k]->is_ok = '合格';
            }elseif ($v->is_ok == 2) {
                $list[$k]->is_ok = '不合格';
            }else{
                $list[$k]->is_ok = '未处理';
            }




        }
        $result['total'] = $res->total();
        $result['rows'] = $list;
        echo json_encode($result);
        }else{
            return view('admin.xz.xzindex');
        }

    
    }


     public function xzadd(Request $request)
    {
        if($request->isMethod('post')){
            $this->validate($request, [
                'name' => 'required',
                'unit_id' => 'required',
                'up_unit' => 'required',
                'gclb' => 'required',
                'gcsx' => 'required',
                'fxdj' => 'required',
                'fhjb' => 'required',
                'bjsj' => 'required',
                'spsj' => 'required',
                'yssj' => 'required',
                'is_ok' => 'required',
            ],$this->messages('jrjg'));
            $input = $request->all();
            if($input['unit_id'] == ''){
                return ['status'=>0,'msg'=>'请选择申请的单位'];
            }else{

                

                    $data = Input::except('_token');
                 
                    $data = array(
                        'user_id' => Auth::guard('admin')->user()->id,
                        'created_at' => date('Y-m-d H:i:s',time()),
                        'updated_at' => date('Y-m-d H:i:s',time()),
                        'name' => $input['name'],
                        'unit_id' => $input['unit_id'],
                        'up_unit' => $input['up_unit'],
                        'fxdj' => $input['fxdj'],
                        'fhjb' => $input['fhjb'],
                        'bjsj' => $input['bjsj'],
                        'spsj' => $input['spsj'],
                        'yssj' => $input['yssj'],
                    
                    );

                    if ($request->gclb != '') {
                         $data['gclb'] = $request->gclb;
                    }

                    if ($request->gcsx != '') {
                        $data['gcsx'] = $request->gcsx;
                    }


                      if ($request->is_ok != '') {
                      
                             $data['is_ok'] = $request->is_ok;
                    }


                foreach ($data as $k=>$v){
                    $data[$k] = htmlspecialchars($v);
                }
              

                     $re = DB::table('jrjg')->insert($data);

                    if ($re) {
                         return ['status'=>1,'msg'=>'添加成功'];
                    }else{
                         return ['status'=>0,'msg'=>'添加失败'];
                    }
            }

        }
        //类型
        $type = db::table('unit_type')->where('is_del','0')->get();
        //市
        $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();
        //获取金融机构的id
        $company = db::table('units')->where('type','=','6')->orderby('id')->get();
        return view('admin.xz.xzadd', ['type'=>$type,'city' => $city,'company'=>$company]);
    }

    public function xzedit($id,Request $request){



        $re = DB::table('jrjg')->where('jrjg_id',$id)->first();
        //获取单位
        $unit = DB::table('units')->where('id',$re->unit_id)->first();
        //获取省市区
        $ci = db::table('area')->where('id',$unit->city)->first();
        $ar = db::table('area')->where('id',$unit->area)->first(); 
        $or = db::table('area')->where('id',$unit->org_id)->first(); 

        if($request->isMethod('post')){
            $this->validate($request, [
                'name' => 'required',
                'unit_id' => 'required',
                'up_unit' => 'required',
                'gclb' => 'required',
                'gcsx' => 'required',
                'fxdj' => 'required',
                'fhjb' => 'required',
                'bjsj' => 'required',
                'spsj' => 'required',
                'yssj' => 'required',
                'is_ok' => 'required',
            ],$this->messages('jrjg'));
            $input = $request->all();
            $data = Input::except('_token');

                 
            $datas = array(
                'user_id' => Auth::guard('admin')->user()->id,
                
                'updated_at' => date('Y-m-d H:i:s',time()),
                'name' => $input['name'],
                'unit_id' => $input['unit_id'],
                'up_unit' => $input['up_unit'],
            
                'fxdj' => $input['fxdj'],
                'fhjb' => $input['fhjb'],
                'bjsj' => $input['bjsj'],
                'spsj' => $input['spsj'],
                'yssj' => $input['yssj'],
               
            );


           
            if ($request->gclb != '') {
                 $datas['gclb'] = $request->gclb;
            }

            if ($request->gcsx != '') {
                $datas['gcsx'] = $request->gcsx;
            }


              if ($request->is_ok != '') {
              
                     $datas['is_ok'] = $request->is_ok;
            }


            foreach ($datas as $k=>$v){
                $datas[$k] = htmlspecialchars($v);
            }


            $upxz = DB::table('jrjg')->where('jrjg_id',$id)->update($datas);

            if ($upxz) {
                 return ['status'=>1,'msg'=>'修改成功'];
            }else{
                 return ['status'=>0,'msg'=>'修改失败'];
            }

        }

        //类型
        $type = db::table('unit_type')->where('is_del','0')->get();
        //市
        $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();
        //获取金融机构的id
        $company = db::table('units')->where('type','=','6')->orderby('id')->get();
        return view('admin.xz.xzedit', ['id'=>$id,'type'=>$type,'city' => $city,'company'=>$company,'re'=>$re,'unit'=>$unit,'ci'=>$ci,'ar'=>$ar,'or'=>$or]);

    }

    public function findcompany(Request $request){

            $id['id'] = $request->id;
            $company = db::table('units')->where($id)->first();

            //获取省市区
            $citys['id'] = $company->city;
            $city = db::table('area')->where($citys)->first();


            $areas['id'] = $company->area;
            $area = db::table('area')->where($areas)->first();

            $orgs['id'] = $company->org_id;
            $org = db::table('area')->where($orgs)->first();

            $json = array('company'=>$company,'city'=>$city,'area'=>$area,'org'=>$org);
            echo json_encode($json);
    }

    //行政许可相关资料
    public function xzup($id,Request $request){


        if($request->rows) {


        $res = DB::table('jrjg_uptype')->paginate($request->rows);
        $list = $res->items();

        $result['total'] = $res->total();
        $result['rows'] = $list;
      
        echo json_encode($result);
        }else{
            return view('admin.xz.xzup',['id'=>$id]);
        }
    }

    //添加多文件
    public function add_zxup($id,$jrid,Request $request){
         if($request->isMethod('post')){

             $filedir = "upload/jrjg/".date('Y-m-d',time())."/";
             $datas = Input::except('_token');

               if (!$request->file('pic')) {
                 return ['status'=>0,'msg'=>'至少添加一个材料'];
             }else{

            
                foreach ($request->file('pic') as $file) {
                 $extension = $file->getClientOriginalExtension();
                 $newImagesName=md5(time()).random_int(1,100000).".".$extension;
                 $file->move($filedir,$newImagesName);
                 $path = $filedir.$newImagesName;
                 $datas =array(
                    'wj_url' => $path,
                    'user_id' => Auth::guard('admin')->user()->id,
                    'created_at' => date('Y-m-d H:i:s',time()),
                    'up_id' => $id,
                    'jrjg_id' => $jrid,
                    'other_type' => $extension,
                    );
             
                    $res = DB::table('jrjg_wj')->insert($datas);
                 }


              if($res){
                     return ['status'=>1,'msg'=>'添加成功'];
                }else{
                     return ['status'=>0,'msg'=>'添加失败'];
                }
            }
        }
        //获取类型
        $uptype = DB::table('jrjg_uptype')->where('up_id',$id)->first();
        return view('admin.xz.add_zxup', ['id'=>$id,'jrid'=>$jrid,'uptype'=>$uptype]);
    }

    public function edit_zxup($id,$jrid,Request $request){

        if($request->isMethod('post')){

             $filedir = "upload/jrjg/".date('Y-m-d',time())."/";
             $datas = Input::except('_token');

             if (!$request->file('pic')) {
                 return ['status'=>0,'msg'=>'至少新增一个材料'];
             }else{
                 foreach ($request->file('pic') as $file) {
                 $extension = $file->getClientOriginalExtension();
                 $newImagesName=md5(time()).random_int(1,100000).".".$extension;
                 $file->move($filedir,$newImagesName);
                 $path = $filedir.$newImagesName;
                 $datas =array(
                    'wj_url' => $path,
                    'user_id' => Auth::guard('admin')->user()->id,
                    'created_at' => date('Y-m-d H:i:s',time()),
                    'up_id' => $id,
                    'jrjg_id' => $jrid,
                    'other_type' => $extension,
                    );
             
                    $res = DB::table('jrjg_wj')->insert($datas);
                    }


                   if($res){
                         return ['status'=>1,'msg'=>'新增成功'];
                    }else{
                         return ['status'=>0,'msg'=>'新增失败'];
                    } 

             }
        }


          //获取相关图片
             $map = array(
                'jrjg_id' => $jrid,
                'up_id' => $id,
             );

          $re = DB::table('jrjg_wj')->where($map)->get();
          $count = DB::table('jrjg_wj')->where($map)->count();



          $uptype = DB::table('jrjg_uptype')->where('up_id',$id)->first();
          return view('admin.xz.edit_zxup', ['id'=>$id,'jrid'=>$jrid,'re'=>$re,'uptype'=>$uptype,'count'=>$count]);



    }

    public function delxz($id,Request $request){

        $re = db::table('jrjg')->where('jrjg_id',$id)->delete();
        if($re){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>0,'msg'=>'删除失败'];
        }
        return redirect()->back();


    }


      public function view($id,Request $request){
        $re = DB::table('jrjg')->where('jrjg_id',$id)->first();
        //获取单位
        $unit = DB::table('units')->where('id',$re->unit_id)->first();
        //获取省市区
        $ci = db::table('area')->where('id',$unit->city)->first();
        $ar = db::table('area')->where('id',$unit->area)->first(); 
        $or = db::table('area')->where('id',$unit->org_id)->first();
        //类型
        $type = db::table('unit_type')->where('is_del','0')->get();
        //市
        $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();
        //获取金融机构的id
        $company = db::table('units')->where('type','=','6')->orderby('id')->get();
        return view('admin.xz.view', ['id'=>$id,'type'=>$type,'city' => $city,'company'=>$company,'re'=>$re,'unit'=>$unit,'ci'=>$ci,'ar'=>$ar,'or'=>$or]);
        



    }

 

}
